import Vue from 'vue'
import App from './App.vue'
import router from './router'
import { registerApplication, start } from "single-spa";

Vue.config.productionTip = false

async function loadScript(url) {
  return new Promise((resolve, reject) => {
    let script = document.createElement('script')
    script.src = url
    script.onload = resolve;
    script.onerror = reject
    document.head.appendChild(script)
  })
}

// 样式不隔离， 没有js沙箱的机制
// singleSpa 不够灵活， 不能动态加载组件

registerApplication('myVueapp', async () => {
  // systemJs
  await loadScript('http://localhost:10000/js/chunk-vendors.js')
  await loadScript('http://localhost:10000/js/app.js')
  return window.singleVue;

}, location => location.pathname.startsWith('/vue')) // 用户切换到/vue路径下，我需要加载子应用

start() // 开启

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
